@echo off
chcp 65001 > nul
echo ===================================================================
echo   One-click Rebuild, Upload, and Deploy simpledaohang Service
echo ===================================================================
echo.

REM ===================================================================
REM ||                  CONFIGURATION VARIABLES                      ||
REM ===================================================================

REM --- Basic Settings ---
set IMAGE_NAME=liured88/simpledaohang
set TAR_DIR=d:\temp\tar
set CONTAINER_NAME=simpledaohang
set NETWORK_NAME=mynet
set CONTAINER_PORT=7586
set APP_INTERNAL_PORT=7531

REM --- Remote Server Settings ---
REM !!! IMPORTANT: Please fill in your remote server details here !!!
set REMOTE_USER=gpzx
set REMOTE_HOST=172.29.173.42
set REMOTE_TAR_DIR=/bak/tar

REM --- Advanced: Remote Docker Commands Configuration ---

REM This variable defines the absolute path on the REMOTE SERVER for your uploads volume.
REM You MUST ensure this directory exists on the remote server. E.g., run: ssh your_user@your_ip "mkdir -p /data/simpledaohang/uploads"
set REMOTE_VOLUME_PATH=/d/docker/simpedaohang/uploads

set DOCKER_STOP_CMD=docker stop %CONTAINER_NAME%
set DOCKER_REMOVE_CMD=docker rm %CONTAINER_NAME%

REM This command cleans up unused Docker images on the remote server to save space.
set DOCKER_PRUNE_CMD=docker image prune -f

REM The full docker run command. All environment variables (-e) and volumes (-v) are configured here.
set DOCKER_RUN_CMD=docker run -d ^
  --name %CONTAINER_NAME% ^
  --network %NETWORK_NAME% ^
  -p %CONTAINER_PORT%:%APP_INTERNAL_PORT% ^
  -v "%REMOTE_VOLUME_PATH%:/app/uploads" ^
  -e DB_HOST=mysql ^
  -e DB_PORT=3306 ^
  -e DB_NAME=navigation_db ^
  -e DB_USER=myadmin ^
  -e APP_PORT=%APP_INTERNAL_PORT% ^
  -e APP_HOST=0.0.0.0 ^
  %IMAGE_NAME%

REM ===================================================================
REM ||                      SCRIPT LOGIC                             ||
REM ||             (Usually no need to modify below)                 ||
REM ===================================================================

set TAR_FILE=%TAR_DIR%\%IMAGE_NAME:/=__%.tar
set DEPLOY_SCRIPT_LOCAL=deploy_temp.sh
set DEPLOY_SCRIPT_REMOTE=%REMOTE_TAR_DIR%/deploy.sh

echo [Step 1/7] Check and create local temp directory...
if not exist "%TAR_DIR%" (
    mkdir "%TAR_DIR%"
    echo Created directory: %TAR_DIR%
) else (
    echo Directory exists: %TAR_DIR%
)
echo.

echo [Step 2/7] Build Docker image...
docker build -t %IMAGE_NAME% .
if %errorlevel% neq 0 (
    echo ERROR: Docker image build failed!
    pause
    exit /b 1
)
echo Docker image built successfully!
echo.

echo [Step 3/7] Save Docker image to tar file...
echo Saving image to %TAR_FILE%...
docker save -o "%TAR_FILE%" %IMAGE_NAME%
if %errorlevel% neq 0 (
    echo ERROR: Docker image save failed!
    pause
    exit /b 1
)
echo Image saved successfully!
echo.

echo [Step 4/7] Upload image to remote server...
scp "%TAR_FILE%" %REMOTE_USER%@%REMOTE_HOST%:%REMOTE_TAR_DIR%/
if %errorlevel% neq 0 (
    echo ERROR: Image upload failed!
    echo TIP: Make sure OpenSSH client is installed and passwordless SSH is configured.
    pause
    exit /b 1
)
echo Image uploaded successfully!
echo.

echo [Step 5/7] Create deployment script locally...
(
    echo #!/bin/bash
    echo set -e
    echo echo "[Remote] Starting deployment..."
    echo cd %REMOTE_TAR_DIR%
    echo echo "[Remote] Loading Docker image from tar file..."
    echo docker load -i %IMAGE_NAME:/=__%.tar
    echo echo "[Remote] Stopping and removing old container..."
    echo %DOCKER_STOP_CMD% 2^>^&1 ^|^| true
    echo %DOCKER_REMOVE_CMD% 2^>^&1 ^|^| true
    echo echo "[Remote] Pruning old images..."
    echo %DOCKER_PRUNE_CMD% 2^>^&1 ^|^| true
    echo echo "[Remote] Starting new container with configured command..."
    echo %DOCKER_RUN_CMD%
    echo echo "[Remote] Deployment completed!"
    echo docker ps ^| grep %CONTAINER_NAME%
) > "%DEPLOY_SCRIPT_LOCAL%"
echo Deployment script "%DEPLOY_SCRIPT_LOCAL%" created successfully.
echo.

echo [Step 6/7] Upload deployment script to remote server...
scp "%DEPLOY_SCRIPT_LOCAL%" %REMOTE_USER%@%REMOTE_HOST%:%DEPLOY_SCRIPT_REMOTE%
if %errorlevel% neq 0 (
    echo ERROR: Failed to upload deployment script!
    pause
    exit /b 1
)
echo Deployment script uploaded successfully!
echo.

echo [Step 7/7] Execute deployment script on remote server...
ssh %REMOTE_USER%@%REMOTE_HOST% "dos2unix %DEPLOY_SCRIPT_REMOTE% && chmod +x %DEPLOY_SCRIPT_REMOTE% && %DEPLOY_SCRIPT_REMOTE%"
if %errorlevel% neq 0 (
    echo ERROR: Remote deployment execution failed!
    echo TIP: Ensure 'dos2unix' is installed on the remote server (sudo apt-get install dos2unix).
    pause
    exit /b 1
)
echo Remote deployment executed successfully!
echo.

REM Cleanup local temporary script
if exist "%DEPLOY_SCRIPT_LOCAL%" (
    del "%DEPLOY_SCRIPT_LOCAL%"
)

echo ===================================================================
echo                        ALL DONE!
echo ===================================================================
echo.
echo  Access your application at: http://%REMOTE_HOST%:%CONTAINER_PORT%
echo.
echo  To view logs on server, run:
echo    ssh %REMOTE_USER%@%REMOTE_HOST% "docker logs %CONTAINER_NAME%"
echo.
echo  To check status on server, run:
echo    ssh %REMOTE_USER%@%REMOTE_HOST% "docker ps -a"
echo.
pause